

var pwFlag = false;

$(document).ready(function(){
	
	initResPw();
	initChanSubmit();
});

function initResPw(){
	var pwPat = /^(\w){6,20}$/;			//密码正则表达式
	
	$("#pw1").blur(function(){
		var pw = $(this).val();
		
		if(pw != ""){
			if(!pwPat.test(pw)){
				showErr("pwValidMsg","请输入6-20位字母数字或下划线");
				pwFlag = false;
			}else{
        		$("#pwValidMsg").css("visibility","hidden");
        		pwFlag = true;
			}
		}else{
			$("#pwValidMsg").css("visibility","hidden");
			pwFlag = false;
		}
		
	});
	
	$("#pw2").blur(function(){
		var pw1 = $("#pw1").val();
		var pw2 = $(this).val();
		var pw1vis = $("#pwValidMsg").css("visibility");
		
		//pw1显示错误信息是，Pw2不进行错误提示
		
		if(pw2==""){
			pwFlag = false;
			return;
		}
		
		if(pw1 != pw2 && pw1vis != "visible"){
			showErr("pwReValidMsg","两次输入密码不一致");
			pwFlag = false;
		}else if(pw1 != pw2){
			pwFlag = false;
		}else{
			$("#pwReValidMsg").css("visibility","hidden");
			pwFlag = true;
		}
		
	});
}

function initChanSubmit(){
	$("#chanPwSubmit").click(function(){
		
		if(pwFlag){
			
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"chanPwAction",
		        data: {
		        	"user.password":$("#pw1").val()
		        },
		        success:function(data,status) {
		        	
		        	if(data=="success"){
			        	var t = 5;
			        	timer=setInterval(function(){	//倒计时功能，这个好
			        		if(t==0){
			        			clearInterval(timer);
			        			window.location.href="login.jsp"; 
			        		}else{
					        	showCorrect("chanSecSpan","修改成功，"+(t--)+"秒之后返回登录界面");
			        		}
			        	},1000);
		        	}else{
		        		showErr("chanSecSpan","填写有误或发生未知错误");
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}
		//validate register message 
		validateRegMsg();
	});
}

function showCorrect(id,msg){
	$("#"+id).text(msg);
	$("#"+id).removeClass("errorMessage").addClass("correctMessage");
	$("#"+id).css("visibility","visible");
}

function showErr(id,msg){
	$("#"+id).text(msg);
	$("#"+id).removeClass("correctMessage").addClass("errorMessage");
	$("#"+id).css("visibility","visible");
}

